package com.ruoyi.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.github.pagehelper.PageInfo;
import com.ruoyi.domain.dto.HotListDto;
import com.ruoyi.domain.dto.LendMessageDto;
import com.ruoyi.utils.PageByList;
import com.ruoyi.common.core.page.PageDomain;
import com.ruoyi.common.core.page.TableSupport;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.domain.LendMessage;
import com.ruoyi.service.ILendMessageService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 图书借阅Controller
 *
 * @author qyb
 * @date 2023-12-26
 */
@RestController
@RequestMapping("/book/lend")
public class LendMessageController extends BaseController
{
    @Autowired
    private ILendMessageService lendMessageService;

    /**
     * 查询图书借阅列表
     */
    @PreAuthorize("@ss.hasPermi('book:lend:list')")
    @GetMapping("/list")
    public TableDataInfo list(LendMessage lendMessage)
    {
        startPage();
        List<LendMessage> list = lendMessageService.selectLendMessageList(lendMessage);
        return getDataTable(list);
    }
    /**
     * 查询借阅记录(查询所有借阅信息，每一本书的被借阅历史，每一个人的借阅历史)
     */
    @GetMapping ("/getLendMessage")
    public TableDataInfo getLendMessage(LendMessageDto lendMessageDto)
    {
        startPage();
        List<LendMessageDto> list = lendMessageService.getLendMessage(lendMessageDto);
        return getDataTable(list);
    }

    /**
     * 查询借书热榜
     */
    @GetMapping("/getHotList")
    public TableDataInfo getHotList( LendMessageDto lendMessageDto)
    {
        List<HotListDto> list = lendMessageService.getHotList(lendMessageDto);
        return getDataTable(list);
    }
    /**
     * 查询快到期图书
     */
    @GetMapping("/getExpiring")
    public TableDataInfo getExpiring( LendMessageDto lendMessageDto)
    {
        startPage();
        List<LendMessageDto> list = lendMessageService.getExpiring(lendMessageDto);
        return getDataTable(list);
    }
    /**
     * 借阅图书
     */
    @PostMapping("/lendBook")
    @Log(title = "图书借阅", businessType = BusinessType.INSERT)
    public AjaxResult lendBook(@RequestBody LendMessage lendMessage) {
        return lendMessageService.lendBook(lendMessage);
    }

    /**
     * 归还图书
     */
    @PostMapping ("/returnBook")
    @Log(title = "图书归还", businessType = BusinessType.RETUTN)

    public AjaxResult returnBook( @RequestBody @Validated LendMessage lendMessage)
    {
        return lendMessageService.returnBook(lendMessage);
    }

    /**
     * 逻辑删除借阅记录
     */
    @PostMapping ("/updateIsDelete")
    @Log(title = "图书借阅记录删除", businessType = BusinessType.DELETE)
    public AjaxResult updateIsDelete(@RequestBody String lendIds)
    {
        return lendMessageService.updateIsDelete(lendIds);
    }

    /**
     * 提交丢失申请
     */
    @PostMapping ("/lossReport")
    public AjaxResult lossReport(@RequestBody @Validated LendMessage lendMessage)
    {
        return lendMessageService.lossReport(lendMessage);
    }

    /**
     * 续借图书
     */
    @PostMapping ("/renewBook")
    @Log(title = "图书续借", businessType = BusinessType.INSERT)
    public AjaxResult renewBook(@RequestBody @Validated LendMessage lendMessage)
    {
        return lendMessageService.renewBook(lendMessage);
    }

    /**
     * 导出图书借阅列表
     */
    @PreAuthorize("@ss.hasPermi('book:lend:export')")
    @Log(title = "图书借阅", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, LendMessageDto lendMessageDto)
    {
        List<LendMessageDto> list = lendMessageService.getLendMessage(lendMessageDto);
        ExcelUtil<LendMessageDto> util = new ExcelUtil<LendMessageDto>(LendMessageDto.class);
        util.exportExcel(response, list, "图书借阅数据");
    }

    /**
     * 获取图书借阅详细信息
     */
    @PreAuthorize("@ss.hasPermi('book:lend:query')")
    @GetMapping(value = "/{lendId}")
    public AjaxResult getInfo(@PathVariable("lendId") Long lendId)
    {
        return success(lendMessageService.selectLendMessageByLendId(lendId));
    }

    /**
     * 新增图书借阅
     */
    @PreAuthorize("@ss.hasRole('admin') or @ss.hasRole('teacher')")
//    @PreAuthorize("@ss.hasPermi('book:lend:add')")
    @Log(title = "图书借阅", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody LendMessage lendMessage)
    {
        return lendMessageService.insertLendMessage(lendMessage);
    }

    /**
     * 修改图书借阅
     */
    @PreAuthorize("@ss.hasRole('admin') or @ss.hasRole('teacher')")
//    @PreAuthorize("@ss.hasPermi('book:lend:edit')")
    @Log(title = "图书借阅", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody @Validated LendMessage lendMessage)
    {
        return lendMessageService.updateLendMessage(lendMessage);
    }

    /**
     * 删除图书借阅
     */
    @PreAuthorize("@ss.hasRole('admin') or @ss.hasRole('teacher')")
//    @PreAuthorize("@ss.hasPermi('book:lend:remove')")
    @Log(title = "图书借阅", businessType = BusinessType.DELETE)
    @DeleteMapping("/{lendIds}")
    public AjaxResult remove(@PathVariable Long[] lendIds)
    {
        return toAjax(lendMessageService.deleteLendMessageByLendIds(lendIds));
    }
}
